package store.lunangangster.bpmn.converter;

import javax.xml.stream.XMLStreamReader;
import store.lunangangster.bpmn.model.BpmnModel;
import store.lunangangster.bpmn.model.EventBasedGateway;
import store.lunangangster.bpmn.util.BpmnXmlUtil;

public class EventBasedGatewayConverter extends GatewayConverter {

  @Override
  protected EventBasedGateway convertToElement(XMLStreamReader xsr, BpmnModel model) throws Exception {
    EventBasedGateway eventBasedGateway = new EventBasedGateway();
    boolean instantiate = false;
    String eventGatewayType = ATTRIBUTE_VALUE_EXCLUSIVE;

    String instantiateValue = xsr.getAttributeValue(null, ATTRIBUTE_VALUE_INSTANTIATE);
    if (ATTRIBUTE_VALUE_TRUE.equals(instantiateValue)) {
      instantiate = true;
    }
    String eventGatewayTypeValue = xsr.getAttributeValue(null, ATTRIBUTE_GATEWAY_EVENT_TYPE);
    if (ATTRIBUTE_VALUE_PARALLEL.equals(eventGatewayTypeValue)) {
      eventGatewayType = ATTRIBUTE_VALUE_PARALLEL;
    }

    eventBasedGateway.setInstantiate(instantiate);
    eventBasedGateway.setEventGatewayType(eventGatewayType);
    BpmnXmlUtil.addXmlLocation(xsr, eventBasedGateway);

    parseChildElement(xsr, eventBasedGateway, getTargetName(), model);
    return eventBasedGateway;
  }

  @Override
  public String getTargetName() {
    return ELEMENT_GATEWAY_EVENT;
  }
}
